package com.gcb.controller;

import com.gcb.constant.MessageConstant;
import com.gcb.entity.Result;
import com.gcb.pojo.CheckGroup;
import com.gcb.pojo.CheckItem;
import com.gcb.pojo.Setmeal;
import com.gcb.service.SetMealService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 35235
 * @Date 2021/8/11 11:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @DubboReference
    private SetMealService setMealService;

    @RequestMapping("/getAllSetmeal.do")
    public Result getAllSetmeal() {
        System.out.println("进入移动端的setmealcontroller");
        try {
            List<Setmeal> list = setMealService.findAll();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setMealService.findById(id);
            Integer[] ids = setMealService.findCheckGroupIdsBySetMealId(id);
            System.out.println(Arrays.toString(ids));
            List<CheckGroup> checkGroupsByIds = setMealService.findCheckGroupsByIds(ids);
            System.out.println(checkGroupsByIds);
            for (CheckGroup checkGroup : checkGroupsByIds) {

                    List<CheckItem> checkItems = setMealService.findCheckItemsById(checkGroup.getId());
                System.out.println(checkItems);
                    checkGroup.setCheckItems(checkItems);

            }
            setmeal.setCheckGroups(checkGroupsByIds);

            System.out.println(setmeal.getCheckGroups());
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }
}
